//
//  MSHomeVC.m
//  MS
//
//  Created by He Yuan on 13-2-6.
//  Copyright (c) 2013年 He Yuan. All rights reserved.
//

#import "MSHomeVC.h"

#import "MSProShowerVC.h"
#import "MSHairDesignVC.h"
#import "MSBeatufulDIYVC.h"
#import "MSSearchPriceVC.h"
#import "MSNearByVC.h"

#import "MSHomeBottomView.h"
#import "MSHomeTagsView.h"
#import "UIColor+ColorUtils.h"

@interface MSHomeVC ()<UIScrollViewDelegate>
{
    UIScrollView *topScrollView;
    UIPageControl *topPageControl;
}
@property(nonatomic,retain)UIScrollView *topScrollView;
@property(nonatomic,retain)UIPageControl *topPageControl;
@end

@implementation MSHomeVC
@synthesize topScrollView = _topScrollView;
@synthesize topPageControl = _topPageControl;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


-(void)loadView
{
    [super loadView];
    
    UIButton *topBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *topImg = [UIImage imageNamed:@"home_topbar"];
    UIImage *topWordsImg = [UIImage imageNamed:@"home_topbar_words"];
    [topBtn setImage:topWordsImg forState:UIControlStateNormal];
    [topBtn setImage:topWordsImg forState:UIControlStateHighlighted];
    [topBtn setBackgroundImage:topImg forState:UIControlStateNormal];
    [topBtn setBackgroundImage:topImg forState:UIControlStateHighlighted];
    [topBtn setFrame:CGRectMake(0,0,topImg.size.width, topImg.size.height)];
    [self.view addSubview:topBtn];
    
    CGRect scrollViewFrame = CGRectMake(0,40,320.0,139.0);
    _topScrollView = [[UIScrollView alloc]initWithFrame:scrollViewFrame];
    [_topScrollView setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
    _topScrollView.delegate = self;
    _topScrollView.pagingEnabled = YES;
    _topScrollView.bounces = NO;
    _topScrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_topScrollView];
    [self.view sendSubviewToBack:self.topScrollView];
    
    _topPageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, scrollViewFrame.size.height+scrollViewFrame.origin.y - 10,scrollViewFrame.size.width,0)];
    if ([self.topPageControl respondsToSelector:@selector(setCurrentPageIndicatorTintColor:)])
    {
         [_topPageControl setCurrentPageIndicatorTintColor:[UIColor colorWithString:@"#f376a0"]];
         [_topPageControl setPageIndicatorTintColor:[UIColor colorWithString:@"#fbeef4"]];
    }
    [self.view addSubview:_topPageControl];
    
    [self addTopAdsView];
    
    NSArray *tags = [NSArray arrayWithObjects:@"热门发型",@"BOBO头",@"日韩",@"流行",@"更多>>", nil];
    CGRect tagsFrame = CGRectMake(0,scrollViewFrame.origin.y+scrollViewFrame.size.height, 320, 30);
    MSHomeTagsView *tagsView = [[MSHomeTagsView alloc]initWithFrame:tagsFrame tags:tags];
    [self.view addSubview:tagsView];
    
    CGFloat bottomViewHeight = [UIScreen mainScreen].applicationFrame.size.height - self.tabBarController.tabBar.frame.size.height - scrollViewFrame.size.height - topImg.size.height - tagsFrame.size.height + 4 ;
    MSHomeBottomView *bottomView = [[MSHomeBottomView alloc]initWithFrame:CGRectMake(0, tagsFrame.size.height+tagsFrame.origin.y, 320, bottomViewHeight) withDelegate:self];
    [self.view addSubview:bottomView];
}

-(void)addTopAdsView
{
    CGRect scrollViewFrame = self.topScrollView.frame;
    NSInteger totalAds = 3;
    for (int i = 0 ; i < totalAds; i++)
    {
        CGRect btnFrame = CGRectMake(i*scrollViewFrame.size.width, 0,scrollViewFrame.size.width,scrollViewFrame.size.height);
        UIButton *imgbtn = [MSTool createButton:btnFrame action:@selector(imageButtonHot:) delegate:self];
        [imgbtn setImage:[UIImage imageNamed:@"home_topdemo"] forState:UIControlStateNormal];
        [imgbtn setImage:[UIImage imageNamed:@"home_topdemo"] forState:UIControlStateHighlighted];
        [self.topScrollView addSubview:imgbtn];
    }
    self.topPageControl.numberOfPages = totalAds;
    self.topPageControl.currentPage = 0;
    [self.topScrollView setContentSize:CGSizeMake(totalAds*scrollViewFrame.size.width, 0)];
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [(TabBarViewController *)self.tabBarController setCustomTabBarHidden:NO];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if (![self.modalViewController class]) {
        [self.navigationController setNavigationBarHidden:NO animated:YES ];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setTitle:nil showBackButton:NO];
}

- (void)configureViews:(BOOL)animation
{
    [super configureViews:animation];
}

-(void)bottomViewButtonFuction:(UIButton *)btn
{
    
}

//专业发型秀
-(void)ProfressHairShowerAction:(UIButton *)btn
{
    [self loadViewController:NSStringFromClass([MSProShowerVC class]) hidesBottomBarWhenPushed:YES];   
}

//发型设计
-(void)HairDesignAction:(UIButton *)btn
{
    [self loadViewController:NSStringFromClass([MSHairDesignVC class]) hidesBottomBarWhenPushed:YES];
}


//百变染发
-(void)BaiBianRuanFaAction:(UIButton *)btn
{

}


//扮美DIY
-(void)BanMeiDIYAction:(UIButton *)btn
{
    [self loadViewController:NSStringFromClass([MSBeatufulDIYVC class]) hidesBottomBarWhenPushed:YES];
}

//查价位
-(void)SearchPriceAction:(UIButton *)btn
{
    [self loadViewController:NSStringFromClass([MSSearchPriceVC class]) hidesBottomBarWhenPushed:YES];
}

//周边
-(void)NearByAction:(UIButton *)btn
{
    [self loadViewController:NSStringFromClass([MSNearByVC class]) hidesBottomBarWhenPushed:YES];
}


-(void)imageButtonHot:(UIButton *)btn
{
    
}

-(void)clickme:(UIButton *)btn
{
    [self loadViewController:NSStringFromClass([MSProShowerVC class]) hidesBottomBarWhenPushed:YES];
}


#pragma mark
#pragma UIScrollViewDelegate methords
- (void)scrollViewDidScroll:(UIScrollView *)_selfScV
{
    CGFloat pageWidth = _selfScV.frame.size.width;
    int page = floor((_selfScV.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.topPageControl.currentPage = page;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
//    HYLog(@"begindrag");
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//    HYLog(@"enddecelerating");
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    
    self.topScrollView = nil;
    self.topPageControl = nil;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
